// pages/evaluationDetail/evaluationDetail.js
const { httpPost, httpGet } = require('../../common/js/http')
const WxParse = require('../../../wxParse/wxParse');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUseGetUserProfile: app.globalData.canIUseGetUserProfile,
    pageData: {},
    showBtn: 'buy' //根据不同状态展示按钮
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('app.globalData.canIUseGetUserProfile',app.globalData.canIUseGetUserProfile)
    // if (wx.getUserProfile) {
    //   this.setData({
    //     canIUseGetUserProfile: true
    //   })
    // }
    console.log('app.globalData.userInfo',!app.globalData.userInfo)
    if(!app.globalData.userInfo){
      this.setData({
        showBtn: 'login'
      })
    }
    if(options.id){
      // httpPost({
      httpGet({
        url: "/app/exam/ordinary/getExamDetailById",
        data: {examId: options.id},
        isLoding: true,
        lodingMsg: '加载中...'
      }).then(res => {
        console.log(res)
        if(res.errorCode == 0){
          this.setData({
            pageData: res.data,
            contentTxt: WxParse.wxParse('contentTxt', 'html', res.data.examDesc, this, 0),
          })
        
        }
      })
    }
    this.setData({
      examId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let sharImg = this.data.pageData.shareIcon || this.data.pageData.examIcon+'!/both/200x160'
    return {
      title: this.data.pageData.examName,
      path: '/pages/evaluation/evaluationDetail/evaluationDetail?id=' + this.data.examId,
      imageUrl: sharImg,
      success: res => {
      },
      fail: res => {
        console.log(res, '转发失败')
      }
    }
  },
  toPage(){
    wx.navigateTo({
      url: '../../order/confirmOrder/confirmOrder?examId=' + this.data.pageData.examReport.examId + '&examMarketPrice=' + this.data.pageData.examMarketPrice + '&examPrice=' + this.data.pageData.examPrice + "&examIcon=" + this.data.pageData.examIcon + "&examName=" + this.data.pageData.examName
    })
  },
  bindGetUserInfo (e) {
    if(app.globalData.canIUseGetUserProfile){
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: res => {
          this.setData({
            isLogin: true
          })
          //静默授权
          app.getMemberId({encryptedData:res.encryptedData,iv:res.iv},this.toPage.bind(this))
        }
      })
    }else{
      wx.getUserInfo({
        success: res => {
          // 可以将 res 发送给后台解码出 unionId
          //静默授权
          app.getMemberId({encryptedData:res.encryptedData,iv:res.iv},this.toPage.bind(this))
          // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
          // 所以此处加入 callback 以防止这种情况
          if (app.userInfoReadyCallback) {
            app.userInfoReadyCallback(res)
          }
        },
      })
      if(res.userInfo.nickName&&res.userInfo.avatarUrl){
        this.setData({
          isLogin: true
        })
      }
    }
  }
})